画出一个icon icon可以放在标签上也可以放在按钮上,但是 大多数情况是放的图片
package cn.usts.edu.lesson04;
import javax.swing.*;
import java.awt.*;
public class JIconDemo extends JFrame implements Icon {
// 设置icon宽高
private int width,height;
// 全参构造
public JIconDemo(int width, int height) {
this.width = width;
this.height = height;
}
// 无参构造
public JIconDemo() {
}
public void init(){
= new JIconDemo(20,20);
JIconDemo jIconDemo // 图标可以放按钮上也可以放标签上
JLabel jLabel = new JLabel("iconDemo",jIconDemo,SwingConstants.CENTER);
this.setBounds(100,100,500,500);
this.setVisible(true);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
Container container = this.getContentPane();
.add(jLabel);
container
}
public static void main(String[] args) {
new JIconDemo().init();
}
// 画icon
@Override
public void paintIcon(Component c, Graphics g, int x, int y) {
.fillOval(x,y,width,height);
g}
@Override
public int getIconWidth() {
return this.width;
}
@Override
public int getIconHeight() {
return this.height;
}
}